package cn.guozeyiblog.blog;

import java.util.ArrayList;

import me.maxwin.view.XListView.IXListViewListener;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import cn.guozeyiblog.blog.utils.Content;
import cn.guozeyiblog.blog.utils.NetTools;
import cn.guozeyiblog.blog.utils.Statictool;

public class HotXListView implements IXListViewListener, OnItemClickListener{
	
	private Handler handler;
	private Context context;
	private static final int HOTLOAD = 3;
	private static final int HOTUPDATE = 4;
	private static int count=1;
	private static boolean Refresh=false;
	
	public HotXListView( Handler handler,Context context) {
		// TODO Auto-generated constructor stub
		this.context =context;
		this.handler = handler;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
        ArrayList<String> link = MyPagerAdapter.getHotlink();
		//Toast.makeText(context, link.get(position-1), 1).show();
		Intent intent = new Intent(context, Article.class);
		intent.putExtra("Uri", link.get(position-1));
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		new Thread(){
			public void run() {
				ArrayList<Content> dataArrayList = NetTools.DoHttp(Statictool.HOTWEB);
				for (Content c:dataArrayList) {
					System.out.println("Title:"+c.gettitle()+"--Link:"+c.getLink());
				}		
				Message msg = new Message();
				msg.what = HOTUPDATE;
				msg.obj = dataArrayList;
				handler.sendMessage(msg);
			};
		}.start();
		//第一次刷新时让Count+1
		if (!Refresh) {
			count++;
		}
		Refresh = true;
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		new Thread(){
			public void run() {
				ArrayList<Content> dataArrayList = NetTools.DoHttp(Statictool.HOTWEB+"&page="+count);
				System.err.println(Statictool.HOTWEB+"?page="+count);
				for (Content c:dataArrayList) {
					System.out.println("Title:"+c.gettitle()+"--Link:"+c.getLink());
				}
				Message msg = new Message();
				msg.what = HOTLOAD;
				msg.obj = dataArrayList;
				handler.sendMessage(msg);
				count++;
			};
		}.start();
	}

}
